import { useRouter } from 'vue-router'
import { RequestRegister } from '@/api/login'
import { ref } from 'vue'
import { userStore } from '@/stores/user'
import { Toast } from 'vant'
/**
 * 登录业务
 */
export const userRegister = () => {
    const store = userStore()
    const router = useRouter()
    const username = ref('')
    const password = ref('')
    const onSubmit = async values => {
        let data = await RequestRegister(values.username,values.password)
        let { resultCode,resultInfo,token } = data
        if(resultCode == 1){
            // 保存用户信息
            store.saveUser({nick:resultInfo.nick,headerimg:resultInfo.headerimg}) 
            // 保存token
            localStorage.setItem('TOKEN',token)
            // localStorage.getItem(token)

            Toast.success('注册成功')
            // 跳转回登录页面
            router.replace({path:'/login'})
        }
        else {
            Toast.fail('用户名或密码出错！')
        }
    }
    const onClickLeft = () => {
        history.back()
    }
    return {
        onSubmit,
        onClickLeft,
        username,
        password,
    }
}